318D - Ants - CodeForces Solution


dfs and similar *2000

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <cassert>

using namespace std;


const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int max_side = 100;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, t; cin >> n >> t;

    vector<vector<int>> g(2 * max_side + 1, vector<int>(2 * max_side + 1, 0));
    int C = 2 * max_side + 1;
    g[max_side][max_side] = n;

    queue<int> q; // pos -> num;
    if(n >= 4) q.push(max_side * C + max_side);
    while(!q.empty()){
        int cx = q.front() / C, cy = q.front() % C;
        q.pop();

        if(g[cx][cy] < 4) continue;

        g[cx][cy] -= 4;
        if(g[cx][cy] >= 4) q.push(cx * C + cy);

        for(int d = 0; d < 4; d ++){
            int nx = cx + dirs[d][0], ny = cy + dirs[d][1], npos = nx * C + ny;
            assert(0 <= nx && nx < g.size() && 0 <= ny && ny < g.size());
            g[nx][ny] += 1;
            if(g[nx][ny] >= 4) q.push(npos);
        }
    }

    while(t --){
        int x, y; cin >> x >> y;
        x += max_side, y += max_side;
        if(x < 0 || x >= g.size() || y < 0 || y >= g.size()) cout << 0 << '\n';
        else cout << g[x][y] << '\n';
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping